const pool = require("./db.js");

// 获取所有会议室列表
async function getAllRooms() {
  const [rows] = await pool.execute('SELECT * FROM meeting_room');
  return rows;
}

// 根据ID获取会议室
async function getRoomById(id) {
  const [rows] = await pool.execute('SELECT * FROM meeting_room WHERE id = ?', [id]);
  return rows[0];
}

// 添加新会议室
async function addMeetingRoom(room) {
  const { roomName, roomType, seatCount, hasProjector, hasSound, hasInternet, pricePerHour } = room;
  const [result] = await pool.execute(
    'INSERT INTO meeting_room (room_name, room_type, seat_count, has_projector, has_sound, has_internet, price_per_hour) VALUES (?, ?, ?, ?, ?, ?, ?)',
    [roomName, roomType, seatCount, hasProjector, hasSound, hasInternet, pricePerHour]
  );
  return result.insertId;
}

// 更新会议室状态
async function updateRoomStatus(id, status) {
  const [result] = await pool.execute(
    'UPDATE meeting_room SET status = ? WHERE id = ?',
    [status, id]
  );
  return result.affectedRows > 0;
}

// 删除会议室
async function deleteMeetingRoom(id) {
  const [result] = await pool.execute(
    'DELETE FROM meeting_room WHERE id = ?',
    [id]
  );
  return result.affectedRows > 0;
}

module.exports = {
  getAllRooms,
  getRoomById,
  addMeetingRoom,
  updateRoomStatus,
  deleteMeetingRoom
};